package com.etc.controller;

import com.etc.common.MyBaseController;
import com.etc.vo.StudentVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

@Controller
public class StudentController extends MyBaseController {

    /**
     * 初始化 注册界面
     * @return
     */
    @GetMapping("regist.action")
    public ModelAndView init(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();

        // 怎么获取容器中的内容SpringContext对象
        Properties ps = (Properties) this.applicationContext.getBean("usermap");
        mav.setViewName("regist");
//        mav.addObject("usermap", ps); 默认是请求的转发
        request.getServletContext().setAttribute("usermap", ps);
        return mav;
    }

    /**
     * 注册界面 注册按钮点击
     * POST regist.action     phone=input&password=input
     * @return
     */
    @PostMapping("regist.action")
    public ModelAndView regist(StudentVo vo, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();

        // 怎么获取容器中的内容SpringContext对象
        Properties ps = (Properties) this.applicationContext.getBean("usermap");
        // application对象
        ServletContext application = request.getServletContext();
        // 获取IP地址
        String ip = request.getRemoteAddr();
        vo.setIp(ip);

        if (ps.containsKey(vo.getPhone())) { // 电话号存在 userlist.properties中

            // 生成随机的token
            String uuid = UUID.randomUUID().toString();
            mav.addObject("token", uuid); // request.setAttribute
            vo.setToken(uuid); // 集合中的vo添加token

            // 把配置文件中的用户名取出来赋值
            vo.setUname(ps.get(vo.getPhone()).toString());
            if (application.getAttribute("stulist") != null) { // 判断是否是第一次进来
                List<StudentVo> stulist = (List<StudentVo>) application.getAttribute("stulist");
                int flg = 0;
                for (int i = 0; i < stulist.size(); i++) {
                    StudentVo item = stulist.get(i);

                    if (item.getPhone().equals(vo.getPhone())) {
                        flg++;
                        item.setToken(vo.getToken());
                    }
                }
                if (flg == 0) { // 没改过
                    stulist.add(vo);
                }


            } else {
                List<StudentVo> stulist = new ArrayList<>();
                stulist.add(vo);
                application.setAttribute("stulist", stulist);
            }
            mav.addObject("message", vo.getPhone() + "注册成功");
            mav.setViewName("login");
        } else {

            mav.addObject("message", "电话号不合法");
            mav.setViewName("regist");
        }

        return mav;
    }

    // login.action?uname=tom&password=123&token=lkkdfjsklda
    // 返回值就是 viewname
    // 参数中加入模型Model model
    @PostMapping("login.action")
    public String login(StudentVo vo, Model model, HttpServletRequest request) {

        // 获取注册完的学生集合
        List<StudentVo> volist = (List<StudentVo>) request.getServletContext().getAttribute("stulist");
        int flg = 0;
        int flg1 = 0;
        if (volist != null) {
            for (StudentVo studentVo : volist) {
                if (vo.getPhone().equals(studentVo.getPhone()) && vo.getPassword().equals(studentVo.getPassword())) {
                    if (vo.getToken().equals(studentVo.getToken())) {
                        request.getSession().setAttribute("loginuser", studentVo);
                        flg1++;
                    }
                    flg++;
                }
            }
        }

        if (flg == 0) { // 遍历之后没有匹配的用户名密码
            model.addAttribute("message", "用户名密码不正确"); // modelandview中的model
            return "login";
        } else if (flg1 == 0) { // 令牌过期
            model.addAttribute("message", "令牌过期重新注册"); // modelandview中的model
            return "regist";
        } else { // 成功

            return "redirect:jsp_welcome.action"; // 默认是转发， 想重定向
        }
    }

}
